// pages/mine/albumView.js

const app = getApp()
const { wxPost, wxGet, formatTime, commonShare, host, getJwt } = require('../../utils/common.js')
import { $wuxGallery } from '../../lib/index'

Page({
  data: {
    page: 0,
    limit: 10,
    pageObj: null,
    imgs: null,
    urls:null,
    isGuest:true
  },
  onShow:function(){
    if (app.globalData.uploadImg){
      this.setData({ page: 0 })
      this.loadImgs()
      app.globalData.uploadImg=false
    }
  },
  onLoad(options) {
    const { albumId:id,name } = options
    
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2] //上一个页面
    var accountPage = pages[pages.length - 3] //上一个页面

    if (prevPage) {
      this.setData({ id, name, infoId: prevPage.data.infoId, isGuest: accountPage && accountPage.data.isGuest })

    }else{
      this.setData({ id, name })
    }
    this.loadImgs()
  },
  showGallery(e) {
    const { current } = e.currentTarget.dataset
    const { urls, imgs,isGuest,infoId} = this.data
    const that=this
    this.$wuxGallery = $wuxGallery()
    this.$wuxGallery.show({
      current,
      urls,
      [`delete`]: (current, urls) => {
        wx.showModal({
          title: '提示',
          content: '确定要删除吗？',
          success: function (sm) {
            if (sm.confirm) {
              if(isGuest){
                wx.showToast({
                  title: '当前账户为测试帐号,不能上传图片哦!',
                  icon:'none',
                  duration:2000
                })
                return 
              }
              wx.showLoading({
                title: '删除中...',
              })
              const { id } = imgs[current]
              
              wxPost('/assetsInfo/feedBackImg/remove', { id ,infoId}, ({ data }) => {
                // urls.splice(current, 1)
                // imgs.splice(current, 1)
                // const { pageObj}=that.data
                // if (pageObj.totalRecord - 1 <= 10) {
                //   pageObj.haxNextPage = false
                // }
                // that.setData({
                //   urls, imgs, pageObj
                // })
                if(data && data['errorCode']===0){
                  that.loadImgs()
                  var pages = getCurrentPages()
                  var prevPage = pages[pages.length - 2] //上一个页面              
                  if (prevPage) {
                    prevPage.loadAlbumList()
                  }
                }else{
                  wx.showToast({
                    title: data['msg']||'删除失败',
                    icon:'none'
                  })
                }
              }, null, () => {
                wx.hideLoading()
              })
            }
          }
        })
        return true
      },
      cancel() {
        //console.log('Close gallery')
      },
      onTap(current, urls) {
        //console.log(current, urls)
        return true
      },
      onChange(e) {
        //console.log(e)
      },
    })
  },
  loadImgs: function (msg) {
    const that = this
    that.setData({
      loading: true
    })
    wx.showLoading({
      title: '图片加载中...',
      mask:true
    })
    const { page, limit, id } = this.data
    wxGet(`/assetsInfo/imgList/${id}`, { start: page * 10, limit }, ({ data }) => {
      if (data.errorCode === 0) {
        if (data.list.length === 0) {
          that.setData({
            text: '',
            imgs: [],
            pageObj: {}
          })
        } else {
          that.setData({
            text: '',
            imgs: page ? that.data.imgs.concat(data.list) : data.list,
            pageObj: data.pageObj
          })
          let urls=[]
          that.data.imgs.forEach((o)=>{
            urls.push(o.review)
          })
          that.setData({
            urls
          })
        }
      }
      setTimeout(() => {
        this.setData({
          loading: false
        })
      }, 1000)
    },null,()=>{
      wx.hideLoading()
    })
  },
  onPullDownRefresh: function (options) {
    const { current } = this.data
    wx.showNavigationBarLoading()
    this.setData({ page: 0 })
    this.loadImgs()
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },
  onReachBottom: function (options) {
    // const { pageObj, page } = this.data
    // if (pageObj && pageObj.haxNextPage) {
    //   this.setData({
    //     page: page + 1
    //   })
    //   wx.showNavigationBarLoading()
    //   this.loadImgs()
    //   wx.hideNavigationBarLoading()
    // } else {
    //   wx.showToast({
    //     title: '没有更多数据了',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }
  },
  loadMoreImgs:function(){
    const { pageObj, page } = this.data
    if (pageObj && pageObj.haxNextPage) {
      this.setData({
        page: page + 1
      })
      this.loadImgs()
    } 
  },
  uploadBtnClick:function(){
    if (this.data.isGuest) {
      wx.showToast({
        title: '测试帐号,不能上传图片哦!',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const { id,infoId} = this.data
    wx.navigateTo({
      url: `uploadImg?albumId=${id}&infoId=${infoId}`
    })
  }
})